<template>
  <div class="city-pay-bar" :id="id"></div>
</template>
<script>
  import echarts from 'echarts/lib/echarts'
  import 'echarts/lib/chart/bar'
  import 'echarts/lib/component/legend'
  import 'echarts/lib/component/tooltip'
  import { mapActions, mapState} from 'vuex'

  export default {
    props: {
      id: {
        type: String,
        required: true
      }
    },
    computed: {
      ...mapState(['cityPay'])
    },
    data () {
      return {
        legendData: ['实时','昨日','七日平均']
      }
    },
    watch: {
      cityPay (val, oldval) {
        this.onReady()
      }
    },
    mounted () {
      this.onReady()
    },
    methods: {
      onReady () {
        const { id, cityPay, legendData } = this
        const option = {
          title: {
            text: '各城市支付数据',
            textStyle: {
              color: '#ffffff',
              fontWeight: 'normal',
              fontSize: 16
            },
            top: 0,
            left: '4%'
          },
          legend: {
            itemHeight: 10,
            data: legendData,
            textStyle: {
              color: '#485e81'
            },
            top: 0,
            right: '3%'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '4%',
            right: '3%',
            top: '22%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            data: cityPay.xAxis,
            axisLabel: {
              rotate: 45,
              textStyle: {
                color: '#52bfc9'
              }
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            z: 10
          },
          yAxis: {
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'solid',
                color: ['rgba(27, 29, 31, 1)']
              }
            },
            axisLabel: {
              textStyle: {
                color: '#52bfc9'
              }
            }
          },
          dataZoom: [
            {
              type: 'inside',
              start: 0,
              end: 40,
            }
          ],
          series: [
/*            { // For shadow
              type: 'bar',
              itemStyle: {
                normal: {color: '#1b1d1f'}
              },
//              barGap:'-100%',
              barCategoryGap:'40%',
              data: dataShadow,
              animation: false
            },*/
            {
              name: legendData[0],
              type: 'bar',
              barWidth: '20%',
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      {offset: 0, color: '#4993b5'},
                      {offset: 1, color: '#4f8c93'}
                    ]
                  ),
                  barBorderRadius: 20
                },
                emphasis: {
                }
              },
              data: cityPay.dataToday
            },
            {
              name: legendData[1],
              type: 'bar',
              barWidth: '20%',
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      {offset: 0, color: '#39a8f5'},
                      {offset: 1, color: '#329ff8'}
                    ]
                  ),
                  barBorderRadius: 20
                },
                emphasis: {
                }
              },
              data: cityPay.dataYesterDay
            },
            {
              name: legendData[2],
              type: 'bar',
              barWidth: '20%',
              itemStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                      {offset: 0, color: '#91679e'},
                      {offset: 1, color: '#766cb3'}
                    ]
                  ),
                  barBorderRadius: 20
                },
                emphasis: {
                }
              },
              data: cityPay.dataAvg
            }
          ],
          color: ['#64a28a', '#41b5f7', '#af6286']
        }
        let bar = echarts.init(document.getElementById(id))
        bar.setOption(option, true)
        window.addEventListener('resize', function () {
          bar.resize()
        })
      }
    }
  }
</script>
<style type="text/css" scoped>
  .city-pay-bar{
    width: 100%;
    height: 39%;
  }
</style>
